<title>KV列表</title>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>内容管理</cite></a>
        <a><cite>KV列表</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body layui-form">
            <div class="layui-form-item">
                <div class="layui-input-inline">
                    <input type="text" name="keywords" placeholder="请输入关键词" class="layui-input">
                </div>
                <div class="layui-input-inline">
                    <button type="button" class="layui-btn" lay-submit lay-filter="LAY-content-kv-search"> <i class="layui-icon layui-icon-search"></i> </button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="LAY-content-kv-manage" lay-filter="LAY-content-kv-manage"></table>
            <script type="text/html" id="LAY-content-kv-toolbar">
                <div class="layui-btn-container">
                    {{# if (AppGeneral.userCan('content.kv.destroy')) { }}
                    <btton class="layui-btn layui-btn-sm layui-btn-danger" lay-event="deletes"><i class="layui-icon layui-icon-delete"></i> </btton>
                    {{# } }}
                    {{# if (AppGeneral.userCan('content.kv.create')) { }}
                    <btton class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-1"></i> </btton>
                    {{# } }}
                </div>
            </script>
            <script type="text/html" id="LAY-content-kv-tool">
                <div class="layui-btn-group">
                    {{# if (AppGeneral.userCan('content.kv.edit')) { }}
                    <btton class="layui-btn layui-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></btton>
                    {{# } }}
                </div>
            </script>
        </div>
    </div>
</div>

<script>
    layui.use(['admin', 'baseMethods'], function () {
        const {table,layer,form,baseMethods} = layui, tableId = 'LAY-content-kv-manage';
        const tableIns = baseMethods.table({
            elem: '#'+tableId,
            url: '/content/kv/list',
            toolbar: '#LAY-content-kv-toolbar',
            cols: [[
                {checkbox: true,fixed: true},
                {field: 'sort', title: '排序', width: 100, sort: !0},
                {field: 'thumb', title: '图片', event: 'preview', unresize: true, width: 90,
                    templet: function (data) {
                        return '<img src="'+AppGeneral.storage(data.thumb)+'" alt="" width="30" height="30" onerror="AppGeneral.imgError(this)">'
                    }
                },
                {field: 'name', title: '标题'},
                {field: 'status', title: '状态', unresize: true, width: 100, align: 'center',
                    templet: function (data) {
                        return parseInt(data.status) === 1 ? '<span class="layui-badge layui-bg-green">显示</span>' : '<span class="layui-badge layui-bg-orange">隐藏</span>';
                    }
                },
                {field: 'created_at', title: '添加时间', width: 180},
                {field: 'updated_at', title: '修改时间', width: 180},
                {fixed: 'right', width: 120, align:'center', templet: '#LAY-content-kv-tool'}
            ]]
        });
        const tableTools = {
            add: function () {
                baseMethods.adminLayer(
                    {title: '添加', area: ['750px', '500px'], id: 'LAY-popup-content-kv-form'},
                    {tpl: 'content/kv/form', id: 'LAY-content-kv-form'},
                    {
                        url: '/content/kv/store',
                        type: "post",
                        success: function (res) {
                            console.log(res)
                            tableIns.reloadData();
                        }
                    },
                )
            },
            deletes: function () {
                const hasCheck = table.checkStatus(tableId), hasCheckData = hasCheck.data;
                if (hasCheckData.length > 0) {
                    const ids = hasCheckData.map(item => item.id);
                    baseMethods.toolOpera(`确认删除选中的 ${hasCheckData.length} 项吗？`, {
                        url: '/content/kv/destroy',
                        type: "delete",
                        data: {ids},
                        success: function () {
                            tableIns.reloadData();
                        }
                    })
                    return true;
                }
                layer.msg('请选择操作项', {icon: 5});
            },
            edit: function (obj) {
                let {data} = obj;
                baseMethods.adminLayer(
                    {title: '编辑', area: ['750px', '550px'], id: 'LAY-popup-content-kv-form'},
                    {tpl: 'content/kv/form', mode: 'edit', data, id: 'LAY-content-kv-form'},
                    {
                        url: `/content/kv/${data.id}/update`,
                        type: "put",
                        success: function (field) {
                            obj.update(field);
                        }
                    },
                )
            },
            preview: function (obj) {
                let {data} = obj;
                if (!data.thumb) return false;
                layer.photos({
                    area: ['600px', '400px'],
                    photos: { title: "查看", data: [{src: AppGeneral.storage(data.thumb)}] },
                    shade: .01,
                    closeBtn: 1,
                    anim: 5
                });
            }
        };
        table.on('toolbar('+tableId+')', function(obj) {
            tableTools[obj.event] && tableTools[obj.event].call(this);
        });
        table.on('tool('+tableId+')', function (obj) {
            tableTools[obj.event] && tableTools[obj.event].call(this, obj);
        });
        form.on('submit(LAY-content-kv-search)', function(data){
            tableIns.reloadData({
                where: data.field,
                page: {curr:1}
            })
        });
    });
</script>